package com.pandora.tv.view.ServiceTag;

import java.util.ArrayList;
import java.util.Collections;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pandora.tv.R;
import com.pandora.tv.Adapter.ApplicationAdapter;
import com.pandora.tv.Info.ApplicationInfo;
import com.pandora.tv.Info.ItemInfo;
import com.pandora.tv.Info.TagItemInfo;
import com.pandora.tv.Utils.CompareSort;
import com.pandora.tv.Utils.FastBitmapDrawable;

public class ServiceTagAdapter extends ApplicationAdapter {

	private static final int NUM_COLUMNS = 5;

	Holder holder;
	ArrayList<ArrayList<ItemInfo>> mItemInfo = new ArrayList<ArrayList<ItemInfo>>();

	LayoutInflater inflater;
	Context mContext;
	int mTag;

	public ServiceTagAdapter(Context context, int tagType) {
		mContext = context;
		mTag = tagType;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);		

		dataConvert();
		
		setListener(this);
	}

	@Override
	public int getCount() {

		return mItemInfo.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView != null) {

			holder = (Holder) convertView.getTag();
		} else {
			convertView = inflater.inflate(R.layout.tv_list_item, null);
			
			holder = new Holder();
			holder.tagView = (TextView) convertView.findViewById(R.id.tv_list_item_tagtextview);
			holder.linear = (LinearLayout) convertView.findViewById(R.id.tv_list_item_gridlayout);
			holder.textView1 = (TextView) convertView.findViewById(R.id.tv_list_item_grid_textview1);
			holder.textView2 = (TextView) convertView.findViewById(R.id.tv_list_item_grid_textview2);
			holder.textView3 = (TextView) convertView.findViewById(R.id.tv_list_item_grid_textview3);
			holder.textView4 = (TextView) convertView.findViewById(R.id.tv_list_item_grid_textview4);
			holder.textView5 = (TextView) convertView.findViewById(R.id.tv_list_item_grid_textview5);
			
			holder.textView1.setOnClickListener(mItemClickListener);
			holder.textView2.setOnClickListener(mItemClickListener);
			holder.textView3.setOnClickListener(mItemClickListener);
			holder.textView4.setOnClickListener(mItemClickListener);
			holder.textView5.setOnClickListener(mItemClickListener);
			
			holder.textView1.setOnTouchListener(mItemTouchListener);
			holder.textView2.setOnTouchListener(mItemTouchListener);
			holder.textView3.setOnTouchListener(mItemTouchListener);
			holder.textView4.setOnTouchListener(mItemTouchListener);
			holder.textView5.setOnTouchListener(mItemTouchListener);
			convertView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
				
				@Override
				public void onFocusChange(View v, boolean hasFocus) {
					
				}
			});
			convertView.setTag(holder);
		}

		final ArrayList<ItemInfo> list = mItemInfo.get(position);
		final ItemInfo itemInfo = list.get(0);

		if (itemInfo.isTag()) {

			final TagItemInfo info = (TagItemInfo) itemInfo;
		
			holder.tagView.setVisibility(View.VISIBLE);
			holder.tagView.setText(info.getTagTitle());

			holder.linear.setVisibility(View.GONE);
		} else {

			holder.tagView.setVisibility(View.GONE);
			holder.linear.setVisibility(View.VISIBLE);
			holder.textView5.setVisibility(View.INVISIBLE);
			holder.textView4.setVisibility(View.INVISIBLE);
			holder.textView3.setVisibility(View.INVISIBLE);
			holder.textView2.setVisibility(View.INVISIBLE);
			holder.textView1.setVisibility(View.INVISIBLE);

			ApplicationInfo info = null;
			switch (list.size()) {
			case 5:
				info = (ApplicationInfo)list.get(4);
				holder.textView5.setTag(info);
				holder.textView5.setVisibility(View.VISIBLE);
				holder.textView5.setText(info.getTitle());
				holder.textView5.setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(info.getBitmap()), null, null);
			case 4:
				info = (ApplicationInfo)list.get(3);
				holder.textView4.setTag(info);
				holder.textView4.setVisibility(View.VISIBLE);
				holder.textView4.setText(info.getTitle());
				holder.textView4.setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(info.getBitmap()), null, null);
			case 3:
				info = (ApplicationInfo) list.get(2);
				holder.textView3.setTag(info);
				holder.textView3.setVisibility(View.VISIBLE);
				holder.textView3.setText(info.getTitle());
				holder.textView3.setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(info.getBitmap()), null, null);
			case 2:
				info = (ApplicationInfo) list.get(1);
				holder.textView2.setTag(info);
				holder.textView2.setVisibility(View.VISIBLE);
				holder.textView2.setText(info.getTitle());
				holder.textView2.setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(info.getBitmap()), null, null);
			case 1:
				info = (ApplicationInfo) list.get(0);
				holder.textView1.setTag(info);
				holder.textView1.setVisibility(View.VISIBLE);
				holder.textView1.setText(info.getTitle());
				holder.textView1.setCompoundDrawablesWithIntrinsicBounds(null, new FastBitmapDrawable(info.getBitmap()), null, null);
				break;
			default:
				break;
			}
		}

		return convertView;
	}

	private class Holder {
		TextView tagView;
		LinearLayout linear;
		TextView textView1;
		TextView textView2;
		TextView textView3;
		TextView textView4;
		TextView textView5;
	}

	private void dataConvert() {

		mItemInfo.clear();

		ArrayList<ApplicationInfo> mInfo = new ArrayList<ApplicationInfo>(getAppList());

		int appType;

		Collections.sort(mInfo, CompareSort.PRELOAD_COMPARE);

		ArrayList<ItemInfo> sub = new ArrayList<ItemInfo>();

		if (mInfo.size() != 0) {
			appType = mInfo.get(0).getAppType();
			addAppType(appType);

			for (ApplicationInfo info : mInfo) {
				if ( info.isCurrentTag(mTag)) {
					if (info.getAppType() == appType) {
						sub.add(info);
						if (sub.size() >= NUM_COLUMNS) {
							final ArrayList<ItemInfo> subAdd = new ArrayList<ItemInfo>(sub);
							mItemInfo.add(subAdd);
							sub.clear();
						}
					} else {

						if (sub.size() != 0) {

							final ArrayList<ItemInfo> subAdd = new ArrayList<ItemInfo>(sub);
							mItemInfo.add(subAdd);
							sub.clear();
						}
						sub.add(info);

						appType = info.getAppType();
						addAppType(appType);
					}
				}
			}

			if (sub.size() != 0) {
				final ArrayList<ItemInfo> subAdd = new ArrayList<ItemInfo>(sub);
				mItemInfo.add(subAdd);
				sub.clear();
			}
		}
	}

	private void addAppType(int appType) {

		ArrayList<ItemInfo> tag = new ArrayList<ItemInfo>();

		switch (appType) {
		case ApplicationInfo.PRELOAD:
			appType = ApplicationInfo.PRELOAD;
			tag.add(new TagItemInfo(TagItemInfo.TAG_PRELOAD));
			mItemInfo.add(tag);
			break;
		case ApplicationInfo.THIRD_PARTY:
			appType = ApplicationInfo.THIRD_PARTY;
			tag.add(new TagItemInfo(TagItemInfo.TAG_APPS));
			mItemInfo.add(tag);
			break;
		default:
			appType = ApplicationInfo.PRELOAD;
			tag.add(new TagItemInfo(TagItemInfo.TAG_PRELOAD));
			mItemInfo.add(tag);
			break;
		}
	}

	@Override
	public void onNotifyDataChanged() {
		notifyDataSetChanged();
	}

	@Override
	public void notifyDataSetChanged() {
		dataConvert();
		super.notifyDataSetChanged();
	}
	
	OnClickListener mItemClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			ApplicationInfo info  = ((ApplicationInfo) v.getTag());			
		}
	};
	
	OnTouchListener mItemTouchListener = new OnTouchListener() {
		
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			return false;
		}
	};
}
